import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import UserInfo from '../components/user/UserInfo.vue'
import Publish from '../components/blog/Publish.vue'
import Modifypwd from '../components/user/Modifypwd.vue'
import ModifyInfo from '../components/user/ModifyInfo.vue'
import SetBlog from '../components/blog/SetBlog.vue'
import ManageComments from '../components/blog/ManageComments.vue'
import Categoary from '../components/blog/Categoary.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/', redirect: '/login' },
  { 
    path: '/home', 
    component: Home, 
    redirect: 'welcome',
    children: [
      { path: '/welcome', component: Welcome }, 
      { path: '/userinfo', component: UserInfo },
      { path: '/publish', component: Publish },
      { path: '/modifyuserpassword', component: Modifypwd },
      { path: '/modifyuserinfo', component: ModifyInfo },
      { path: '/setblog', component: SetBlog },
      { path: '/managecomments', component: ManageComments },
      { path: '/categoary', component: Categoary }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载导航守卫，三个参数都不能少
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
